//name: GameField
//author: akoyama
//description: Controls the appearance of the field. Also controls all pill and capsule operations. Attempting to implement collision.
//

//HEIGHT is the only value that really needs to be changed, the others can be tweaked according to taste
var FIELD_TOP_ORIGIN = 0;
var FIELD_LEFT_ORIGIN = 0;
var FIELD_HEIGHT = 150;
var FIELD_WIDTH = FIELD_HEIGHT / 1.5;   
var BORDER_WIDTH = FIELD_WIDTH / 40; 

//Pill width is the playable field width divided by 8. Currently, only seven will fit. There is extra room on both sides.
var PILL_WIDTH = (FIELD_WIDTH - (BORDER_WIDTH * 2)) / 8;         
var PILL_HEIGHT = PILL_WIDTH; //for now   
var PILL_BORDER = PILL_WIDTH / 10;

function GameField (){
    //Defines the dimensions of the game field
    var fieldHeight = FIELD_HEIGHT;
    var fieldWidth = FIELD_WIDTH;
    var borderWidth = BORDER_WIDTH;
    var fieldTop = FIELD_TOP_ORIGIN;
    var fieldLeft = FIELD_LEFT_ORIGIN;
    
    //Pill code. Should be moved to Pill.js. Calling them from Pill currently will return undefined since it depends on Pill 
    //being initialized.
    var pillHeight = PILL_HEIGHT;   
    var pillWidth = PILL_WIDTH;
    var pillBorder = PILL_BORDER;
    
    //All boundaries should take into account the game field origin, the border width, and pillwidth.
    //Actually, topBoundary doesn't take into account pill height and it's working.

    //Currently going outside the left about the side of the player.
    var leftBoundary = fieldLeft + borderWidth - (pillWidth/2);    
    
    var rightBoundary = fieldLeft + fieldWidth - (pillWidth*2); //Off by about a pixel, stays on the inside.
    
    var topBoundary = fieldTop + borderWidth;    //Working.
    
    //Currently, the origin is stopped by the border. Falls outside the bottom border.
    var bottomBoundary = fieldTop + fieldHeight - (borderWidth + pillHeight); 

    //Sets the field size, as well as the border size
    $('#gameField').css('height', fieldHeight + 'px').css('width', fieldWidth + 'px').css('border-width', borderWidth + 'px');
    $('#gameField').css('top', fieldTop + 'px').css('left', fieldLeft + 'px');
    
    //This is needed in cases like Game Over, Restart, New Level
    //Deletes pill so there is no color or saved location
    this.clearField = function(){
	$('.pill').remove(); 
        delete pill;  
    };
    
     
    
    
    
    this.drawElement = function (classname, xpos, ypos) {
        var $element = $('<div/>').addClass(classname);
	$element.css('top',ypos+'px').css('left',xpos+'px');
        if(classname === 'pill')
            $element.css('height', pillHeight + 'px').css('width', pillWidth + 'px');
            $element.css('background-color', pill.color).css('border-width', pillBorder + 'px');
	$('#gameField').append($element);   
    };
   
    this.drawPlacedPill = function (newPlacedPill) {
        var $element = $('<div/>').addClass('placedPill');
        $element.css('top', newPlacedPill.yPos +'px').css('left', newPlacedPill.xPos+'px');
        $element.css('height', pillHeight + 'px').css('width', pillWidth, 'px');    
        $element.css('background-color', newPlacedPill.color);
        $('#gameField').append($element);  
    };
    
    //This is necessary because each time the player moves, the old css pill needs to be cleared. Saves the JS version of Pill.
    this.removePill = function() {
        $('.pill').remove();
    };

    var placedPills = [];  //A container of placedPills which stores their location and color.
    
     //removes previous cssPill and draws a new one
    this.movePill = function(direction) {
        ill.move(direction); 
        this.removePill();
        this.drawElement('pill',pill.xPos ,pill.yPos);
    };
    
    //Creates the pill javascript object and calls its drawing
    this.spawnPill = function () {
        myPill = new Pill();
        this.drawElement('pill',myPill.xPos ,myPill.yPos);
    };
    
    this.addPlacedPill = function(newPlacedPill) {
        placedPills.push(newPlacedPill);
        this.drawPlacedPill(newPlacedPill);
    };
     
    //Return functions
    this.getPillWidth = function() {
      return pillWidth;  
    };
    
    //Returns private boundary variables
    this.getBoundary = function(wall) {
        switch(wall) {
            case 'left':
                return leftBoundary;
                break;
            case 'right':
                return rightBoundary;
                break;
            case 'bottom':
                return bottomBoundary;
                break;
            case 'top':
                return topBoundary;
                break;
        }
    };
    
    this.getFieldDimension = function (dimension) {
        var fHeight = fieldHeight;
        var fWidth = fieldWidth;
        switch(dimension) {
            case 'height':
                return fHeight;
                break;
            case 'width':
                return fWidth;
                break;
            default:
                break;
        }
    };
    
};

//THE ONLY COLLISION FUNCTION(eventually)
//will return TRUE if self is about to move in a direction in which they will touch a placed pill
//will return FALSE if self is about to move in a direction in which there is no placed pill
function MovingIntoAnything (self, direction) {


    
//    if(((element1.left + element1.width) > element2.left) &&    //if element1's left side is greater than element2's left
//    ((element1.top + element1.height) > element2.top) &&        //AND element1's top is greater than element 2's top
//    ((element2.left + element2.width) > element1.left) &&       //AND element2's left is greater than element1's left
//    ((element2.top + element2.height) > element1.top)) {        //AND element2''s top is greater than element1's top

        var rightBoundary = gameField.getBoundary('right');   
        var leftBoundary = gameField.getBoundary('left'); 
        var bottomBoundary = gameField.getBoundary('bottom');
        var topBoundary = gameField.getBoundary('top');    //temporary
        
        switch(direction) {
            case 'left':
                //IF the xPos is less than or equal to the leftBoundary
                if(self.xPos <= leftBoundary){
                    return true;
                }
                else {
                    return false;
                }
                break;
            case 'right':
                if(self.xPos >= rightBoundary) {
                    return true;
                }
                else {
                    return false;
                }
                break;
            case 'down':
                if(self.yPos >= bottomBoundary) {
                    return true;
                }
                else {
                    return false;
                }
                break;
            case 'up':
                //temporary
                if(self.yPos <= topBoundary) {
                    return true;
                }
                else {
                    return false;
                }
                break;
        }
  };
